﻿using RazorTemplate.template;
using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;

namespace RazorTemplate
{
	class Program
	{
		static void Main(string[] args)
		{
			var greetings = new  greetings { Recipient = "John", Sender = "Martin" };
			Console.WriteLine(greetings.TransformText().Trim());
			SendEmail();
		}


		public static void SendEmail()
		{
			var htmlContent = (new email { Sender = "Martin", Recipient = "John", Content = "You win!!!" }).TransformText().Trim();

			using (MailMessage mail = new MailMessage())
			{
				mail.To.Add("some@address.com");
				mail.Subject = "example";
				mail.IsBodyHtml = true;

				var htmlView = AlternateView.CreateAlternateViewFromString(htmlContent, new ContentType("text/html"));
				var attachResource = new LinkedResource(new MemoryStream(GetLogo()));
				attachResource.ContentId = "logo";
				htmlView.LinkedResources.Add(attachResource);

				Attachment att = new Attachment(new MemoryStream(GetCertificate()), "certificate.jpg");
				mail.Attachments.Add(att);

				mail.AlternateViews.Add(htmlView);

				using (var client = new SmtpClient())
				{
					client.Send(mail);
				}
			}
		}

		static byte[] GetLogo()
		{
			return GetResource("RazorTemplate.logo.logo.jpg");
		}

		static byte[] GetCertificate()
		{
			return GetResource("RazorTemplate.logo.certificate.jpg");
		}

		static byte[] GetResource(string name)
		{
			var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
			byte[] resource = new byte[0];

			using (var streamReader = new MemoryStream())
			{
				stream.CopyTo(streamReader);
				resource = streamReader.ToArray();
			}

			return resource;
		}
	}
}
